package com.micro.train.service;

import com.micro.train.service.impl.TrainSeatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * 系统启动时的库存初始化服务
 * 确保所有列车都有今天的库存记录
 */
@Component
public class InventoryInitializer implements CommandLineRunner {

    @Autowired
    private TrainSeatServiceImpl trainSeatService;

    @Override
    public void run(String... args) throws Exception {
        try {
            System.out.println("系统启动 - 开始初始化库存...");
            
            // 获取今天的日期
            LocalDate today = LocalDate.now();
            
            // 为所有列车初始化今天的库存窗口
            trainSeatService.rollInventoryWindow(today);
            
            System.out.println("系统启动 - 库存初始化完成，基准日期: " + today);
        } catch (Exception e) {
            System.err.println("系统启动 - 库存初始化失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
